package com.huluobo.a2107jetpackkotlindemo.lifecycle

import android.os.CountDownTimer
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 *  Created by LC on 2023/11/21.
 */
class AdManager(adViewModel: AdViewModel) : LifecycleObserver {
//    var adManagerListener: AdManagerListener? = null

    //倒计时器
    private val countDownTimer = object : CountDownTimer(adViewModel.millisInFuture, 1000) {
        //每当倒计时读秒时
        override fun onTick(millisUntilFinished: Long) {
            Log.i("TagA", "广告剩余时间:${(millisUntilFinished / 1000).toInt()}秒")
//            adManagerListener?.timing((millisUntilFinished / 1000).toInt())
            adViewModel.setAdTimingResult(millisUntilFinished / 1000)//给viewModel中的LiveData进行赋值
        }

        //结束时
        override fun onFinish() {
            Log.i("TagA", "广告结束,准备进入主页面")
//            adManagerListener?.finish()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun start() {
        Log.i("TagA", "开始计时")
        countDownTimer.start()
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun cancel() {
        Log.i("TagA", "计时结束")
        countDownTimer.cancel()
    }
//
//    interface AdManagerListener {
//        fun timing(second: Int)
//        fun finish()
//    }
}